package cn.cdiothree.service.impl;

import cn.cdiothree.dataobject.ProductCart;
import cn.cdiothree.dao.ProductCartMapper;
import cn.cdiothree.enums.ResultEnum;
import cn.cdiothree.exception.SellException;
import cn.cdiothree.service.ProductCartService;
import cn.cdiothree.util.KeyUtil;
import cn.cdiothree.vo.ResultVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author CumThing
 * @since 2020-02-02
 */
@Service
@Slf4j
public class ProductCartServiceImpl extends ServiceImpl<ProductCartMapper, ProductCart> implements ProductCartService {

    @Autowired
    private ProductCartMapper productCartMapper;

    /**
     * 增加或减少购物车商品
     * @param userId
     * @param productId
     * @param count
     * @return
     */
    @Override
    public ResultVo addOrDecrease(Integer userId, Integer productId, Integer count){
//        count=1;
        log.info("用户{}修改商品{}购物车数量为{}",userId,productId,count);
        ProductCart productCart = selectByUserIdAndProductId(userId,productId);
        if (count==0){
            return this.deleteByUserIdAndProductId(userId,productId);
        }else{
            if (productCart==null){
                ProductCart cartItem = new ProductCart();
                cartItem.setCartId(KeyUtil.genUniqueKey());
                cartItem.setUserId(userId);
                cartItem.setProductId(productId);
                cartItem.setQuantity(count);
                cartItem.setCreateTime(LocalDateTime.now());
                cartItem.setUpdateTime(LocalDateTime.now());
                int insert = productCartMapper.insert(cartItem);
                if (insert<1){
                    throw new SellException(ResultEnum.ADD_PRODUCT_FAILED);
                }
            }else {
    //            count = productCart.getQuantity()+count;
                productCart.setQuantity(count);
                productCartMapper.updateById(productCart);
            }
            return this.list(userId);}
    }

//    @Override
//    public ResultVo decrease(Integer userId,Integer productId,Integer count){
////            count= 1;
//            ProductCart productCart = selectByUserIdAndProductId(userId,productId);
//            count = productCart.getQuantity()-count;
//            productCart.setQuantity(count);
//            productCartMapper.updateById(productCart);
//            if (productCart.getQuantity()==0){
//                return this.deleteByUserIdAndProductId(userId,productId);
//            }else{
//                return this.list(userId);
//            }
//    }

    /**
     * 删除购物车商品
     * @param userId
     * @param productId
     * @return
     */
    @Override
    public ResultVo deleteByUserIdAndProductId(Integer userId, Integer productId){
        log.info("用户{}删除购物车商品{}",userId,productId);
        QueryWrapper<ProductCart> deleteWrapper = new QueryWrapper<>();
        deleteWrapper.eq("user_id",userId).eq("product_id",productId);
         productCartMapper.delete(deleteWrapper);
        return this.list(userId);
    }

    /**
     * 返回购物车列表
     * @param userId
     * @return
     */
    @Override
    public ResultVo list (Integer userId){
        QueryWrapper<ProductCart> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<ProductCart> productCart= productCartMapper.selectList(wrapper);
        return ResultVo.success(productCart);
    }

    /**
     * 选择商品
     * @param userId
     * @param productId
     * @return
     */
    @Override
    public ProductCart selectByUserIdAndProductId(Integer userId,Integer productId){
        log.info("用户{}选中购物车商品{}",userId,productId);
        QueryWrapper<ProductCart> wrapper=new QueryWrapper<>();
        wrapper.eq("user_id",userId).eq("product_id",productId);
        return productCartMapper.selectOne(wrapper);
    }
}
